package com.xzan.medusa.common.entity;

import com.xzan.medusa.common.enums.AppErrorCodeEnum;
import com.xzan.medusa.common.enums.AppReturnCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Map;

/**
 * 提供给App的业务结果泛型类
 * @author mahe
 * @date 2016/12/14
 */
@ApiModel(description = "响应结果")
@Data
public class AppBizResult<T> {
    @ApiModelProperty(notes = "错误码",required = true,position = -9)
    private String resultCode = "0";

    @ApiModelProperty(notes = "业务结果",required = true,position = -10)
    private boolean success = false;

    @ApiModelProperty(notes = "错误信息",required = false,position = -8)
    private String errDesc;

    @ApiModelProperty(notes = "结果集",required = false,position = -8)
    private T result;

    @ApiModelProperty(notes = "额外信息",required = false,position = -7)
    private Map<String, Object> extraInfo;
    /**
     * 这个字段仅ios端获取七牛token接口使用，因为ios的阿道说他们那边对上传接口进行了封装，获取七牛token接口如果不设置为这个就无法上传。。。
     */
    private String uptoken;

    public AppBizResult() {
    }

    public static <T> AppBizResult<T> success(T data){
        return create(data);
    }

    public static <T> AppBizResult<T> empty(){
        AppBizResult<T> result = create();
        result.setResultCode(AppReturnCodeEnum.SUCCESS.getCode());
        result.setSuccess(true);
        return result;
    }

    public static <T> AppBizResult<T> error(AppErrorCodeEnum errorCodeEnum) {
        return create(errorCodeEnum);
    }

    public static <T> AppBizResult<T> error(String code, String message) {
        return create(code,message);
    }

    public static <T,S> AppBizResult<T> error(AppBizResult<S> errorBizResult){
        return error(errorBizResult.getResultCode(),errorBizResult.getErrDesc());
    }

    public static <T> AppBizResult<T> paramsError(String msg){return AppBizResult.error(AppErrorCodeEnum.PARAM_ERROR.getErrCode(),msg);
    }

    public static <T> AppBizResult<T> bizError(String msg){
        return AppBizResult.error(AppErrorCodeEnum.BIZ_EXCEPTION.getErrCode(),msg);
    }
    public static <T> AppBizResult<T> systemError(String msg){
        return AppBizResult.error(AppErrorCodeEnum.EXCEPTION.getErrCode(),msg);
    }

    public static <T> AppBizResult<T> create() {
        return new AppBizResult<>();
    }

    public static <T> AppBizResult<T> create(T data) {
        AppBizResult<T> appBizResult = create();
        appBizResult.setResultCode(AppReturnCodeEnum.SUCCESS.getCode());
        appBizResult.setSuccess(true);
        appBizResult.setResult(data);
        return appBizResult;
    }

    public static <T> AppBizResult<T> create(T data, String code, String message) {
        AppBizResult<T> result = create();
        result.setResultCode(AppReturnCodeEnum.SUCCESS.getCode());
        result.setSuccess(true);
        result.setResult(data);
        result.setErrDesc(message);
        return result;
    }

    public static <T> AppBizResult<T> create(boolean isSuccess, String code, String message) {
        AppBizResult<T> result = create();
        if(isSuccess){
            result.setResultCode(AppReturnCodeEnum.SUCCESS.getCode());
        }else {
            result.setResultCode(code);
        }
        result.setSuccess(isSuccess);
        result.setErrDesc(message);
        return result;
    }

    public static <T> AppBizResult<T> create(T data, boolean isSuccess, String code, String message) {
        return create(data,null,isSuccess,code,message);
    }

    public static <T> AppBizResult<T> create(T data, Map<String, Object> extraInfo) {
        AppBizResult<T> appBizResult = create();
        appBizResult.setResultCode(AppReturnCodeEnum.SUCCESS.getCode());
        appBizResult.setErrDesc(AppReturnCodeEnum.SUCCESS.getDesc());
        appBizResult.setSuccess(true);
        appBizResult.setResult(data);
        appBizResult.setExtraInfo(extraInfo);
        return appBizResult;
    }

    public static <T> AppBizResult<T> create(T data, Map<String, Object> extraInfo, boolean isSuccess, String code, String message) {
        AppBizResult<T> appBizResult = create();
        appBizResult.setResult(data);
        appBizResult.setExtraInfo(extraInfo);
        if(isSuccess){
            appBizResult.setResultCode(AppReturnCodeEnum.SUCCESS.getCode());
        }else {
            if(AppErrorCodeEnum.SESSION_EXPIRE.getErrCode().equals(code)){
                appBizResult.setResultCode(code);
            }else{
                appBizResult.setResultCode(AppReturnCodeEnum.FAILURE.getCode());
            }
        }
        appBizResult.setSuccess(isSuccess);
        appBizResult.setErrDesc(message);
        return appBizResult;
    }

    public static <T> AppBizResult<T> create(String code, String message) {
        AppBizResult<T> appBizResult = create();
        if(AppErrorCodeEnum.SESSION_EXPIRE.getErrCode().equals(code)){
            appBizResult.setResultCode(code);
        }else{
            appBizResult.setResultCode(AppReturnCodeEnum.FAILURE.getCode());
        }

        appBizResult.setSuccess(false);
        appBizResult.setErrDesc(message);
        return appBizResult;
    }

    public static <T> AppBizResult<T> create(AppErrorCodeEnum errorCodeEnum) {
        AppBizResult<T> appBizResult = create();
        if(errorCodeEnum.equals(AppErrorCodeEnum.SESSION_EXPIRE)){
    /**
     * 修改为将错误代码更新至ResultCode中 edit by mahe
     */
            appBizResult.setResultCode(errorCodeEnum.getErrCode());
        }else{
            appBizResult.setResultCode(AppReturnCodeEnum.FAILURE.getCode());
        }
        appBizResult.setSuccess(false);
        appBizResult.setErrDesc(errorCodeEnum.getErrMsg());
        return appBizResult;
    }

    public static <T> AppBizResult error(Boolean isSuccess, AppErrorCodeEnum errorCodeEnum){
        AppBizResult<T> result = create();
        if(isSuccess){
            result.setResultCode(AppReturnCodeEnum.SUCCESS.getCode());
        }else {
            result.setResultCode(errorCodeEnum.getErrCode());
        }
        result.setSuccess(isSuccess);
        result.setErrDesc(errorCodeEnum.getErrMsg());
        return result;
    }

}
